package com.tarena.elts.net;

import java.util.List;

import com.tarena.elts.entity.ExamInfo;
import com.tarena.elts.entity.QuestionInfo;
import com.tarena.elts.entity.User;
import com.tarena.elts.service.ExamService;
import com.tarena.elts.service.IdOrPwdException;
import com.tarena.elts.util.Config;

public class ExamServiceAgent implements ExamService {
	private Config config;
	private String host;
	private int port;
	private String sid;

	public void setConfig(Config config) {
		this.config = config;
		this.host = config.getString("ServerIP");
		this.port = config.getInt("ServerPort");
	}

	@Override
	public User login(int id, String pwd) throws IdOrPwdException {
		// TODO Auto-generated method stub
		Request req = new Request("login", new Class[]{ int.class,
				String.class }, new Object[]{ id, pwd });
		req.setSessionID(sid);
		Response res = Utils.remoteCall(host, port, req);
		sid = res.getSessionID();
		if (res.isSuccess()) {
			return (User) res.getValue();
		} else {
			Exception e = res.getE();
			if (e instanceof IdOrPwdException) {
				throw (IdOrPwdException) e;
			} else {
				throw new RuntimeException(e);
			}
		}
	}

	@Override
	public ExamInfo start() {
		// TODO Auto-generated method stub
		Request req = new Request("start", new Class[]{}, new Object[]{});
		req.setSessionID(sid);
		Response res = Utils.remoteCall(host, port, req);
		sid = res.getSessionID();
		if (res.isSuccess()) {
			return (ExamInfo) res.getValue();
		} else {
			Exception e = res.getE();
			throw new RuntimeException(e);
		}
	}

	@Override
	public QuestionInfo getQuestion(int index) {
		// TODO Auto-generated method stub
		Request req = new Request("getQuestion", new Class[] { int.class },
				new Object[] { index });
		req.setSessionID(sid);
		Response res = Utils.remoteCall(host, port, req);
		sid = res.getSessionID();
		if (res.isSuccess()) {
			return (QuestionInfo) res.getValue();
		} else {
			Exception e = res.getE();
			throw new RuntimeException(e);
		}
	}

	@Override
	public void saveUserAnswers(int index, List<Integer> list) {
		// TODO Auto-generated method stub
		Request request = new Request("saveUserAnswers", new Class[] {
				int.class, List.class }, new Object[] { index, list });
		request.setSessionID(sid);
		Response response = Utils.remoteCall(host, port, request);
		if (response.isSuccess()) {
		} else {
			Exception exception = response.getE();
			throw new RuntimeException(exception);
		}
	}

	@Override
	public int over() {
		Request req = new Request("over", new Class[] {}, new Object[] {});
		req.setSessionID(sid);
		Response res = Utils.remoteCall(host, port, req);
		sid = res.getSessionID();
		if (res.isSuccess()) {
			return (Integer) res.getValue();
		} else {
			Exception e = res.getE();
			throw new RuntimeException(e);
		}
	}

	@Override
	public int getScore() {
		Request req = new Request("getScore", new Class[] {}, new Object[] {});
		req.setSessionID(sid);
		Response res = Utils.remoteCall(host, port, req);
		sid = res.getSessionID();
		if (res.isSuccess()) {
			return (Integer) res.getValue();
		} else {
			Exception e = res.getE();
			throw new RuntimeException(e);
		}
	}

}
